# -*- coding: utf-8 -*-
"""
表单
"""
from flask_wtf import FlaskForm
from wtforms import DateField,DateTimeField,RadioField,IntegerField,StringField, SubmitField, SelectField, TextAreaField, ValidationError, HiddenField, \
    BooleanField, PasswordField
from wtforms.validators import InputRequired,DataRequired, Email, Length, Optional, URL
from main.models import Task,Project
from flask_wtf.file import FileRequired,FileField,FileAllowed

class LoginForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired(), Length(1, 20)])
    password = PasswordField('Password', validators=[DataRequired(), Length(1, 128)])
    remember = BooleanField('Remember me')
    submit = SubmitField('Log in')


class SettingForm(FlaskForm):
    name = StringField('发布单位', validators=[DataRequired(), Length(1, 70)])
    username= StringField('用户名', validators=[DataRequired(), Length(1, 70)])
class ProjectForm(FlaskForm):
    name = StringField('项目名称', validators=[DataRequired(), Length(1, 30)])
    submit = SubmitField()

    def validate_name(self, field):
        if Project.query.filter_by(name=field.data).first():
            raise ValidationError('已存在.')
class TaskForm(FlaskForm):
    batch = IntegerField('任务批次', validators=[InputRequired(),DataRequired()])
    project_name = SelectField('项目名称', coerce=int, default=1)
    name=StringField('任务名称', validators=[DataRequired(),DataRequired(), Length(1, 60)])
    ttype=StringField('任务类型', validators=[DataRequired(),DataRequired(), Length(1, 60)])
    status=RadioField('是否完成', choices=[('y', '已完成'), ('n', '进行中')], validators=[InputRequired(),DataRequired(), Length(1, 60)])
    publisher=StringField('发布者', validators=[DataRequired(),Length(1, 60)])
    contact=StringField('联系方式', validators=[DataRequired(),Length(1, 60)])
    nature=StringField('任务性质', validators=[DataRequired(),Length(1, 60)])
    start_time=DateField('开始时间', format='%Y-%m-%d',validators=[DataRequired(),InputRequired()])
    pic_upload=FileField('上传照片压缩包',validators=[FileAllowed(['zip','rar'])])
    video_upload=FileField('上传视频压缩包',validators=[FileAllowed(['zip','rar'])])
    submit = SubmitField()


    def __init__(self, *args, **kwargs):
        print(Project.query.all())
        super(TaskForm, self).__init__(*args, **kwargs)
        self.project_name.choices = [(project.id, project.name)
                                 for project in Project.query.all()]


